//
//  HMMainTabbarController.swift
//  iOSWeibo12
//
//  Created by Admin on 16/9/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMMainTabbarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 自定义tabbar,是只读的,不能直接使用 = 进行赋值
        // KVC 进行替换
        let newTabBar = HMMainTabBar()
        setValue(newTabBar, forKey: "tabBar")
        
        // 准备闭包
        newTabBar.composeClosure = { () -> () in
            if HMUserAccountViewModel.shared.userLogin {
                
//                let view = CZBlurView()
//                view.showIn(controller: self)
                
                let view = HMComposeTypeView.composeTypeView()
                view.show()
                
//                let composeVC = HMComposeViewController()
//                
//                self.present(UINavigationController(rootViewController: composeVC), animated: true, completion: nil)
            } else {
                let oauthVC = HMOauthViewController()
                
                self.present(UINavigationController(rootViewController: oauthVC), animated: true, completion: nil)
            }
        }
        
        // 添加4个子控制器
        addChildViewControllers()
    }
    
    /// 添加4个子控制器
    private func addChildViewControllers() {
        // 设置barBar的TintColor
        tabBar.tintColor = UIColor.orange
        
        // 首页
        let homeTableVC = HMHomeViewController()
        addChildViewController(controller: homeTableVC, title: "首页", imageName: "tabbar_home")
        
        // 消息
        let messageTableVC = HMMessageViewController()
        addChildViewController(controller: messageTableVC, title: "消息", imageName: "tabbar_message_center")
        
        // 发现
        let discoverTableVC = HMDiscoveryViewController()
        addChildViewController(controller: discoverTableVC, title: "发现", imageName: "tabbar_discover")
        
        // 我
        let profileTableVC = HMProfileViewController()
        addChildViewController(controller: profileTableVC, title: "我", imageName: "tabbar_profile")
    }
    
    /// 添加tabBar子控制器
    private func addChildViewController(controller: UIViewController, title: String, imageName: String) {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        
        // 设置选中图片
        let selectedImageName = imageName + "_selected"
        controller.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        // 设置选中文字
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: UIControlState.selected)
        
        let profileNav = UINavigationController(rootViewController: controller)
        addChildViewController(profileNav)
    }
}
